/***************************************************************************************
* File name    :	ALGAes_PR.c
* Function     :	
* Author       : 
* Date         :
* Version      : 
* Description  :    
* ModifyRecord :
*****************************************************************************************/
#include <Utils/Type.h>
#include "../alg_pr/ALGAes_PR.h"
#include <mbedtls/aes.h>

/***********************************************************************************
* Subrountine:	ALGAes_PR_Run
* Function:
* Input:
* Output:
* Description:
* Date:
* ModifyRecord:
**********************************************************************************/
UINT8 ALGAes_PR_Run(UINT8 nType,UINT8 nMode,UINT8* pIn,UINT8* pOut,UINT16 nDataLen,UINT8* pKey,UINT8* pIV)
{
	UINT8 nRet,type;
	UINT8 nKenLen  = 0;
	mbedtls_aes_context aes_ctx;

	switch(nMode)
	{
		case AES128_CBC:
		case AES128_ECB: nKenLen = 16;break;
		case AES192_CBC:
		case AES192_ECB: nKenLen = 24;break;
		case AES256_CBC:
		case AES256_ECB: nKenLen = 32;break;
		default:return 1;
	}
	if(nType == AES_DECRYPT)
	{
		mbedtls_aes_setkey_dec(&aes_ctx, pKey, nKenLen * 8);
		type = MBEDTLS_AES_DECRYPT;
	}
	else
	{
		mbedtls_aes_setkey_enc(&aes_ctx, pKey, nKenLen * 8);
		type = MBEDTLS_AES_ENCRYPT;
	}

	if(nMode == AES128_CBC || nMode == AES192_CBC || nMode == AES256_CBC)
	{
		nRet = mbedtls_aes_crypt_cbc(&aes_ctx, type, nDataLen, pIV, pIn, pOut);	
	}
	else
	{
		while(nDataLen)
		{
			nRet = mbedtls_aes_crypt_ecb(&aes_ctx, type, pIn, pOut);	
			pIn += 16;
			pOut += 16;
			nDataLen -= 16;
		}
		
	}
	if(nRet != 0)
	{
		nRet = 1;
	}
	return nRet;
}
